home *** CD-ROM | disk | FTP | other *** search
- Path: news.magg.net!news
- From: n4mwd@magg.net (Dennis Hawkins)
- Newsgroups: comp.lang.c
- Subject: Re: Why does this work?
- Date: Thu, 22 Feb 1996 05:46:35 GMT
- Organization: M.A.G. Information Services (MAGG.NET)
- Message-ID: <4ggsi6$1fg@dopey.magg.net>
- References: <4g8f7o$adt@ncar.ucar.edu>
- NNTP-Posting-Host: wpb-133.magg.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- rosinski@ra.cgd.ucar.edu (Jim Rosinski) wrote:
-
- >Could someone please explain why the following code works? In particular, I
- >am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
-
- >#include <stdio.h>
- >main()
- >{
- > void sub1(), sub2();
-
- > struct cmndstruct {
- > void (*funcnam)();
- > };
-
- > struct cmndstruct cmndtable[] = {
- > sub1,
- > sub2,
- > NULL
- > };
- > struct cmndstruct *cmndptr;
-
- > for (cmndptr = cmndtable; *(cmndptr->funcnam) != NULL; cmndptr++) {
- > (*(cmndptr->funcnam))();
- > }
- > exit(0);
- >}
-
- >void sub1()
- >{
- > printf("Inside sub1\n");
- >}
-
- >void sub2()
- >{
- > printf("Inside sub2\n");
- >}
-
- There is nothing really all that unusual about your program. The only
- thing that looks a little strange to me was that you have a structure
- with only one field in it. A simple array of pointers to functions
- would be more efficient. As far as the machine knows, that is
- precisely what you have here. I would reccommend that you consider
- spicing it up a bit by using pointers to functions that take
- parameters. Your code here is straightforward. You initialize your
- pointers to functions and then you call them one after the other in a
- for() loop. No sweat.
-
-
- Dennis Hawkins
- n4mwd@amsat.org
-
-